package com.Servlet;

import com.dao.DaoFactory;
import com.entity.reader;
import com.entity.search;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/login")
public class Login_readerServlet extends HttpServlet {
    public Login_readerServlet(){
        super();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-control", "no-cache");
        String userLogin = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = "";
        String email = "";
        System.out.println(userLogin.matches("1[0-9]{10,10}"));
        if(userLogin.matches("1[0-9]{10,10}") == true){
            phone = userLogin;
            System.out.println("phone");
        }
        else {
            email = userLogin;
            System.out.println("email");
        }
        System.out.println("----------------读者登陆-------------");
        System.out.println("用户:" + userLogin);
        System.out.println("密码:" + password);
        System.out.println("------------------------------------");

        HttpSession session = request.getSession();



        reader userman = new reader();
        userman.setPhone(phone);
        userman.setPassword(password);
        userman.setEmail(email);

        try {
            reader entity = DaoFactory.getInstance().getreaderDao().login(userman);
           if(entity != null){

                //执行跳转
                System.out.println("执行跳转");
//                session.setAttribute("user", entity);
                request.setAttribute("user_name",entity.getNickname());
                session.setAttribute("phone",entity.getPhone());

                session.setAttribute("authornum",0); //设置进入book的初始搜索内容为空
                session.setAttribute("booknum",0);

                request.setAttribute("powerlevel",1);

                /*推荐*/
                search bk1 =  DaoFactory.getInstance().getsearchDao().findBybookname("剑来");
                search bk2 =  DaoFactory.getInstance().getsearchDao().findBybookname("雪中悍刀行");
                search bk3 =  DaoFactory.getInstance().getsearchDao().findBybookname("高数");
                search bk4 =  DaoFactory.getInstance().getsearchDao().findBybookname("斗罗大陆");
                search bk5 =  DaoFactory.getInstance().getsearchDao().findBybookname("龙族1·火之晨曦");
                search bk6 =  DaoFactory.getInstance().getsearchDao().findBybookname("斗破苍穹");
                session.setAttribute("bk1",bk1.getBrief_introduction());
                session.setAttribute("bk2",bk2.getBrief_introduction());
                session.setAttribute("bk3",bk3.getBrief_introduction());
                session.setAttribute("bk4",bk4.getBrief_introduction());
                session.setAttribute("bk5",bk5.getBrief_introduction());
                session.setAttribute("bk6",bk6.getBrief_introduction());


                getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
            }
            else {
                //返回给用户“错误”
                System.out.println("错误");
                request.setAttribute("error","用户名或密码错误");
                request.getRequestDispatcher("Login.jsp").forward(request,response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
